package com.lxyk.controller;

import cn.hutool.core.util.PageUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxyk.pojo.Result;
import com.lxyk.pojo.Worker;
import com.lxyk.service.WorkerService;
import com.lxyk.vo.MainMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/worker")
@Slf4j
public class WorkerController {

    @Autowired
    private WorkerService workerService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户点击侧边栏的“客户管理”，处理该请求，将所有的客户信息渲染到“客户列表”页面，最后返回“客户列表”页面
     * @param  model
     * @return 返回“客户列表”页面
     */
    @RequestMapping("/workerList")
    public String page(Worker worker,Model model,Integer current,Integer size){
        if (current == null){
            current = 1;
        }
        if (size == null){
            size = 10;
        }

        IPage<Worker> workerIPage = workerService.queryByWorker(worker, current, size);
        model.addAttribute("pageInfo",workerIPage.getRecords()); //数据
        model.addAttribute("pageNo",workerIPage.getCurrent());//页号
        model.addAttribute("totalPages",workerIPage.getPages());
        /**
         * 分页彩虹算法
         * 此方法来自：https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java
         * 在页面上显示下一页时，常常需要显示前N页和后N页，PageUtil.rainbow作用于此。
         */
        // 上一页 3 4 [5] 6 7 8 下一页
        int[] pageNums = PageUtil.rainbow((int) workerIPage.getCurrent(), (int) workerIPage.getPages(), 6);
        model.addAttribute("pageNums",pageNums);
        return "workerList";
    }

    /*
    * 点击“添加”按钮，跳转到“添加送水工的”页面
    * @return “添加送水工页面”
    * */
    @RequestMapping("/preSaveWorker")
    public String preSaveWorker(){
        return "workerSave";
    }
    /*
    * 新增送水工
    * */
    @PostMapping("/saveWorker")
    public String saveWorker(Worker worker,MultipartFile file){
        //上传文件oss
        String url = workerService.upload(file);
        worker.setWorkerImage(url);
        workerService.saveWorker(worker);
        //拿到redis的长度
        Long size = redisTemplate.opsForList().size("mainMenu");
        if (size < 12){
            MainMenu menu = new MainMenu();
            menu.setWorkerName(worker.getWorkerName());
            menu.setSendWaterCount(0);
            redisTemplate.opsForList().rightPush("mainMenu",menu);
        }
        return "redirect:/worker/workerList";

    }
    /*
    * 修改送水工，数据回显页面
    * */
    @RequestMapping("/preUpdateWorker/{wid}")
    public String preUpdateWorker(@PathVariable("wid") Integer wid,Model model){
        Worker worker = workerService.getWorkerById(wid);
        model.addAttribute("worker",worker);
        return "workerUpdate";

    }
    /*
    * 修改送水工，修改图片
    * */
    @PostMapping("/updateWorker")
    public String updateWorker(Worker worker,MultipartFile file){
        //上传文件oss
        String url = workerService.upload(file);
        worker.setWorkerImage(url);
        workerService.updateWorker(worker);
        return "redirect:/worker/workerList";

    }
    /*
    * 工资调整
    * */
    @PostMapping("/addSalary")
    @ResponseBody
    public Result addSalary(Integer wid,Integer workerSalary){
        UpdateWrapper<Worker> up = new UpdateWrapper<>();
        up.eq("wid",wid).set("worker_salary",workerSalary);
        boolean result = workerService.update(up);
        return Result.ok(result);

    }


}
